import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tiktokadult/hj_page/mine_business/make_money/mine_withdrawal_record_page.dart';
import 'package:tiktokadult/hj_page/mine_business/make_money/widget/record_list_item.dart';
import 'package:tiktokadult/hj_page/mine_business/make_money/withdraw_details_model.dart';
import 'package:tiktokadult/hj_utils/api_service/mine_service.dart';

abstract class MineWithdrawalRecordLogic extends GetxController {
  RefreshController? refreshController;
  int page = 1;

  WithdrawDetailsModel? withdrawDetailsModel;
  final dataSource = [];
  bool isLoading = true;

  @override
  onReady() {
    super.onReady();
    fetchPageData();
  }

  @mustCallSuper
  fetchPageData({bool isRefresh = true}) async {
    if (isRefresh) page = 1;
  }

  Widget instanceChildItem(int index);
}

//提现明细
class WithDrawalRecordController extends MineWithdrawalRecordLogic {
  @override
  fetchPageData({bool isRefresh = true}) async {
    super.fetchPageData(isRefresh: isRefresh);

    final result = await MineService.getWithdrawDetails(pageNumber: page, pageSize: 10);
    isLoading = false;

    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (result?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(result?.list ?? []);
    page += 1;
    update();
  }

  @override
  Widget instanceChildItem(int index) {
    return WithdrawalRecordItem(dataSource[index]);
  }
}

//金币订单明细
class GoldBillRecordController extends MineWithdrawalRecordLogic {
  @override
  fetchPageData({bool isRefresh = true}) async {
    super.fetchPageData(isRefresh: isRefresh);
    final res = await MineService.getBillData(
      pageSize: 15,
      pageNumber: page,
      type: 1,
    );
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.list ?? []);
    page += 1;
    update();
  }

  @override
  Widget instanceChildItem(int index) {
    return GoldRecordItem(dataSource[index]);
  }
}

//充值明细
class RechargeRecordController extends MineWithdrawalRecordLogic {
  @override
  fetchPageData({bool isRefresh = true}) async {
    super.fetchPageData(isRefresh: isRefresh);
    final res = await MineService.getRechargeBill(pageNumber: page, pageSize: 15);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.list ?? []);
    page += 1;
    update();
  }

  @override
  Widget instanceChildItem(int index) {
    return Padding(
      padding: EdgeInsets.only(bottom: 14),
      child: RechargeRecordItem(dataSource[index]),
    );
  }
}

//收益明细
class IncomeRecordController extends MineWithdrawalRecordLogic {
  @override
  fetchPageData({bool isRefresh = true}) async {
    super.fetchPageData(isRefresh: isRefresh);
    final res = await MineService.getIncomeRecord(pageNumber: page, pageSize: 15);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.list ?? []);
    page += 1;
    update();
  }

  @override
  Widget instanceChildItem(int index) {
    return Padding(
      padding: EdgeInsets.only(bottom: 12),
      child: InComeRecordItem(dataSource[index]),
    );
  }
}

MineWithdrawalRecordLogic instanceController(RecordType type) {
  switch (type) {
    case RecordType.bill:
      return GoldBillRecordController();
    case RecordType.withdraw:
      return WithDrawalRecordController();
    case RecordType.recharge:
      return RechargeRecordController();
    case RecordType.income:
      return IncomeRecordController();
    default:
      throw '$type 没有找到';
  }
}
